/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.client;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import listo.client.model.Task;
import listo.client.model.Tasks;
import listo.client.model.operations.DeleteTasksOp;
import listo.utils.logging.Log;
import listo.utils.types.DateTime;

import java.util.concurrent.TimeUnit;

@Singleton
public class CleanupRunner implements Runnable {

    private final ContextManager contextManager;
    private final Preferences preferences;
    private final Log log;

    @Inject
    protected CleanupRunner(ContextManager contextManager, Preferences preferences, AppConfig appConfig, Log log,
                            Executors executors) {
        this.contextManager = contextManager;
        this.preferences = preferences;
        this.log = log;

        log.debug("Starting cleanup runner");
        executors.getCleanupExecutor().scheduleWithFixedDelay(this, appConfig.getAutoRemoverInitial(),
                appConfig.getAutoRemover(), TimeUnit.SECONDS);
    }

    public void run() {
        log.debug("Starting cleanup");
        long now = new DateTime().getTicks();
        Tasks tasksToRemove = new Tasks();
        for (Task task : contextManager.getCurrentContext().getTasks()) {
            DateTime lastUpdated = task.getLastUpdated();
            if (lastUpdated == null) continue;
            if (task.isCompleted()) {
                int time = (int) ((now - lastUpdated.getTicks()) / (24 * 60 * 60 * 1000));
                if (time >= preferences.getAutoRemoveCompletedDays()) {
                    tasksToRemove.add(task);
                    continue;
                }
            }
            if (!task.hasFolders()) {
                int time = (int) ((now - lastUpdated.getTicks()) / (24 * 60 * 60 * 1000));
                if (time >= preferences.getAutoRemoveDeletedDays()) {
                    tasksToRemove.add(task);
                }
            }
        }
        if (tasksToRemove.isEmpty()) {
            log.debug("No expired tasks found");
            return;
        }

        log.debug("Removing %s expired tasks", tasksToRemove.size());
        DeleteTasksOp op = new DeleteTasksOp();
        op.setTasks(tasksToRemove);
        contextManager.addAndRun(op);
    }

}